﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class TestCreatTextNode : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			Transaction transaction = new Transaction(base.Doc, "创建文字标注");
			transaction.Start();
			Result result;
			try
			{
				string strTextNode = "长度:" + 100.5 + "m";
				XYZ startPoint = base.Sel.PickPoint("选择标注点");
				new FilteredElementCollector(base.Doc).OfClass(typeof(TextNoteType));
				SubTransaction subTransaction = new SubTransaction(base.Doc);
				subTransaction.Start();
				TextNote textEle = UnifiedModified.HYCreatTextNote(base.Doc, "仿宋", strTextNode, startPoint);
				base.Doc.Regenerate();
				subTransaction.Commit();
				this.GetTextElementPos(textEle, base.Doc);
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message + ex.StackTrace);
				if ((int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void GetTextElementPos(TextNote textEle, Autodesk.Revit.DB.Document doc)
		{
			double num = (double)doc.ActiveView.Scale;
			XYZ xyz = textEle.BaseDirection.Normalize();
			XYZ xyz2 = textEle.UpDirection.Normalize();
			XYZ coord = textEle.Coord;
			double width = textEle.Width;
			double height = textEle.Height;
			XYZ item = coord;
			XYZ item2 = coord - num * height * xyz2;
			XYZ item3 = coord + num * width * xyz - num * height * xyz2;
			XYZ item4 = coord + num * width * xyz;
			this.CreateLine(new List<XYZ>
			{
				item,
				item2,
				item3,
				item4,
				item
			}, doc);
		}

		public void CreateLine(List<XYZ> lstXYZ, Autodesk.Revit.DB.Document doc)
		{
			try
			{
				for (int i = 0; i < lstXYZ.Count - 1; i++)
				{
					Line line = Line.CreateBound(lstXYZ[i], lstXYZ[i + 1]);
					doc.Create.NewDetailCurve(doc.ActiveView, line);
				}
			}
			catch (Exception)
			{
			}
		}
	}
}
